<template>
    <el-dialog width="410px" :model-value="modelValue" @closed="handleClose">
        <el-radio-group v-if="showRadio" v-model="type">
            <el-radio :disabled="isAfter(planDetail.beginTime)" label="addTo">增加已选点位</el-radio>
            <el-radio label="kickOutPoint">移除已选点位</el-radio>
        </el-radio-group>

        <el-form-item>
            <el-input
                v-model="textarea"
                :rows="10"
                type="textarea"
                placeholder="粘贴点位编码进行快速调整
每个点位一行，格式如下：

CDA-001-001
CDB-015-012
CDA-098-327
                "
            />
        </el-form-item>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="handleClose">取消</el-button>
                <el-button type="primary" @click="submit">确认</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ref, defineProps, defineEmits, computed, reactive } from 'vue';
import { kickOutPoint, addPoint } from '@/api/plan.ts';
import { useStore } from 'vuex';
import { ElMessage, ElMessageBox } from 'element-plus';
import { downLoadCommon, isAfter } from '@/utils/common';

const store = useStore();
const emits = defineEmits<{(e: 'update:modelValue', visible: boolean): void; (e: 'success'): void }>();
const type = ref(null);
const textarea = ref(null);
const props = defineProps({
    modelValue: {
        type: Boolean,
        default: false,
    },
    planDetail: {
        type: Object,
        default: () => null,
    },
    showRadio: {
        type: Boolean,
        default: true,
    },
});
const handleClose = () => {
    textarea.value = null;
    type.value = null;
    emits('update:modelValue', false);
};
const submit = async () => {
    const params = {
        planId: props.planDetail.planId,
        pointCodeList: textarea.value.split(/[(\r\n)\r\n]+/).filter((code) => code),
    };
    let msg, url;
    if (type.value === 'kickOutPoint') {
        msg = await kickOutPoint(params);
    } else {
        let data = await addPoint(params);
        msg = data?.msg;
        url = data?.url;
    }

    if (msg) {
        await ElMessageBox.confirm(msg, '提示', {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
        });
    } else {
        ElMessage.success('操作成功！');
    }

    url && downLoadCommon(url);

    handleClose();
    emits('success');
};
</script>

<style lang="scss" scoped>
.el-form-item {
    margin-bottom: 4px;
}
.el-radio-group {
    margin: 4px 0 24px;
    .el-radio {
        height: auto;
    }
}
</style>
